package org.frame.security.project.system.controller;

import java.io.IOException;

import org.frame.common.core.AjaxResult;
import org.frame.common.core.entity.TokenUser;
import org.frame.common.core.entity.User;
import org.frame.security.common.utils.SecurityUtils;
import org.frame.security.common.utils.ServletUtils;
import org.frame.security.common.utils.file.FileUploadUtils;
import org.frame.security.framework.aspectj.lang.annotation.Log;
import org.frame.security.framework.aspectj.lang.enums.BusinessType;
import org.frame.security.framework.config.FrameConfig;
import org.frame.security.framework.security.service.TokenService;
import org.frame.security.framework.web.controller.BaseController;
import org.frame.security.project.system.domain.SysUser;
import org.frame.security.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 个人信息 业务处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/user/profile")
public class SysProfileController extends BaseController {
	@Autowired
	private ISysUserService userService;

	@Autowired
	private TokenService tokenService;

	/**
	 * 个人信息
	 */
	@GetMapping
	public AjaxResult profile() {
		TokenUser tokenUser = tokenService.getTokenUser(ServletUtils.getRequest());
		User user = tokenUser.getUser();
		AjaxResult ajax = AjaxResult.success(user);
		ajax.put("roleGroup", userService.selectUserRoleGroup(user.getUserName()));
		ajax.put("postGroup", userService.selectUserPostGroup(user.getUserName()));
		return ajax;
	}

	/**
	 * 修改用户
	 */
	@Log(title = "个人信息", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult updateProfile(@RequestBody SysUser user) {
		return toAjax(userService.updateUserProfile(user));
	}

	/**
	 * 重置密码
	 */
	@Log(title = "个人信息", businessType = BusinessType.UPDATE)
	@PutMapping("/updatePwd")
	public AjaxResult updatePwd(String oldPassword, String newPassword) {
		TokenUser tokenUser = tokenService.getTokenUser(ServletUtils.getRequest());
		User user = tokenUser.getUser();
		String userName = user.getUserName();
		String password = user.getPassword();
		if (!SecurityUtils.matchesPassword(oldPassword, password)) {
			return AjaxResult.error("修改密码失败，旧密码错误");
		}
		if (SecurityUtils.matchesPassword(newPassword, password)) {
			return AjaxResult.error("新密码不能与旧密码相同");
		}
		return toAjax(userService.resetUserPwd(userName, SecurityUtils.encryptPassword(newPassword)));
	}

	/**
	 * 头像上传
	 */
	@Log(title = "用户头像", businessType = BusinessType.UPDATE)
	@PostMapping("/avatar")
	public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
		if (!file.isEmpty()) {
			TokenUser tokenUser = tokenService.getTokenUser(ServletUtils.getRequest());
			User user = tokenUser.getUser();
			String userName = user.getUserName();
			String avatar = FileUploadUtils.upload(FrameConfig.getAvatarPath(), file);
			if (userService.updateUserAvatar(userName, avatar)) {
				AjaxResult ajax = AjaxResult.success();
				ajax.put("imgUrl", avatar);
				return ajax;
			}
		}
		return AjaxResult.error("上传图片异常，请联系管理员");
	}
}
